package com.example.laserharp1;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


/*
直连后，切换到function页面， ip是否不变 ？
min按键需测试， ip为上述ip地址，msg为"Open Camera"？
 */

public class Volume extends AppCompatActivity {

    TextView ipaddress, device;
    Button min, down, up, max, back, home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volume);

        initialWork();
        exqListener();

        Thread myThread = new Thread(new MyServer());
        myThread.start();

    }

    private void initialWork(){
        ipaddress = (TextView) findViewById(R.id.IPAddress);
        device = findViewById(R.id.device);
        min = findViewById(R.id.min);
        down = findViewById(R.id.down);
        up = findViewById(R.id.up);
        max = findViewById(R.id.max);
        back = findViewById(R.id.back);
        home = findViewById(R.id.home);

        if (GlobalVariable.ip!=null){
            ipaddress.setText("Target IP: " + GlobalVariable.ip);
        }else {
            ipaddress.setText("NONE");
        }

        if (GlobalVariable.device!=null){
            device.setText("Target IP: " + GlobalVariable.device);
        }else {
            device.setText("Not Connected");
        }
    }

    private void exqListener(){

        min.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BackgroundTask b = new BackgroundTask();
                b.execute("192.168.1.103", "0000");
            }
        });

        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BackgroundTask b = new BackgroundTask();
                b.execute(GlobalVariable.ip, "0001");
            }
        });

        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BackgroundTask b = new BackgroundTask();
                b.execute(GlobalVariable.ip, "0010");
            }
        });

        max.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BackgroundTask b = new BackgroundTask();
                b.execute(GlobalVariable.ip, "0011");
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Volume.this,FunctionPage.class);
                startActivity(intent);
            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Volume.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    class MyServer implements Runnable
    {
        ServerSocket ss;
        Socket mysocket;
        DataInputStream dis;
        String msg;
        Handler handler = new Handler();
        @Override
        public void run() {
            try {
                ss = new ServerSocket(9700);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),"Waiting for client", Toast.LENGTH_SHORT).show();
                    }
                });
                while (true){
                    mysocket = ss.accept();
                    dis = new DataInputStream(mysocket.getInputStream());
                    msg = dis.readUTF();

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (msg.equals("0000")) {
                                Toast.makeText(getApplicationContext(), "laser harp is mute", Toast.LENGTH_SHORT).show();
                            } else if (msg.equals("0001")) {
                                Toast.makeText(getApplicationContext(), "turn the volume down", Toast.LENGTH_SHORT).show();
                            } else if (msg.equals("0010")) {
                                Toast.makeText(getApplicationContext(), "turn the volume up", Toast.LENGTH_SHORT).show();
                            } else if (msg.equals("0011")) {
                                Toast.makeText(getApplicationContext(), "maximum volume", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    class BackgroundTask extends AsyncTask<String, Void, String>
    {
        Socket s;
        DataOutputStream dos;
        String ip, msg;


        @Override
        protected String doInBackground(String... params) {
            ip = params[0];
            msg = params[1];

            try {
                s = new Socket(ip, 9700);
                dos = new DataOutputStream(s.getOutputStream());
                dos.writeUTF(msg);
                dos.close();
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }
    }

}
